package xyz.fuqufyb.aec.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.fuqufyb.aec.data.JsonResult;
import xyz.fuqufyb.aec.entity.Menu;
import xyz.fuqufyb.aec.entity.Role;
import xyz.fuqufyb.aec.service.IMenuService;
import xyz.fuqufyb.aec.service.IRoleMenuService;
import xyz.fuqufyb.aec.service.IRoleService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 用户: fyb
 * @description class: RoleController
 * @date 时间: 2021/1/30 14:27
 */

@RestController

@RequestMapping("/sys/role")
public class RoleController {


    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IRoleMenuService roleMenuService;

    /**
     * 分页查询加 模糊搜索
     *
     * @param maps
     * @return
     */
    @GetMapping("/table")
    public JsonResult getRoleList(@RequestParam Map<String, Object> maps) {
        JsonResult jsonResult = roleService.getRolePage(maps);
        if (jsonResult != null) {
            return jsonResult;
        }
        return JsonResult.getJsonError();
    }

    /**
     * 新增与修改
     *
     * @param role
     * @param act
     * @return
     */
    @PostMapping("/html")
    public JsonResult updateOrAddRole(Role role, @RequestParam("act") String act) {
        if (act.equals("add")) {
            int i = roleService.insertSelective(role);
            if (i > 0) {
                return JsonResult.getJsonSuccess("新增成功");
            }
        } else if (act.equals("update")) {

            int i = roleService.updateByPrimaryKeySelective(role);
            if (i > 0) {
                return JsonResult.getJsonError("修改成功");
            }
        }
        return JsonResult.getJsonError();
    }

    /**
     * 修改 角色状态
     *
     * @param role
     * @return
     */
    @RequestMapping("/update")
    public JsonResult updateRoleStatus(Role role) {
        int i = roleService.updateByPrimaryKeySelective(role);

        if (i > 0) {
            return JsonResult.getJsonError("修改成功");
        }

        return JsonResult.getJsonError();
    }

    /**
     * 角色id 拥有的菜单数据
     * @param roleId
     * @return
     */
    @RequestMapping("/role_menu")
    public  List<Integer> roleMenuById(Integer roleId) {

        List<Integer> menuListRole = menuService.getMenuListRole(roleId);

        return menuListRole;

    }

    /**
     * 遍历 所有的菜单数据
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/menu_tree")
    public List<Menu> menuTree() throws JsonProcessingException {
        List<Menu> menuTree = menuService.getMenuTree();
//        if (!menuTree.isEmpty()) {
//            ObjectMapper objectMapper = new ObjectMapper();
//            return objectMapper.writeValueAsString(menuTree);
//        }
        return menuTree;
    }


    /**
     * 设置role 角色 权限
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("/assign_menu")
    public JsonResult assignMenu(@RequestParam("roleId") Integer roleId,@RequestParam("menuIds") Integer[] menuIds){
        if (roleId == 0 || menuIds.length == 0){
            return JsonResult.getJsonError("权限不能为空");
        }
        int i = roleMenuService.updateRoleMenuById(roleId,Arrays.asList(menuIds));

        if (i>0){
            return JsonResult.getJsonSuccess("更新成功");
        }
        return JsonResult.getJsonError();
    }


}
